package com.gztms.ordermgnt.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @author Zoey
 * @date 2023/5/8 21:54
 * @Description TODO
 **/
public class TypeConverter implements Converter<Integer> {

    private final static String ADULT = "成人";
    private final static String CHILD = "儿童";

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String type = cellData.getStringValue();
        if (ADULT.equals(type)) {
            return 0;
        }else if(CHILD.equals(type)) {
            return 1;
        }
        return null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new WriteCellData<>(" ");
        }else if (value == 0) {
            return new WriteCellData<>(ADULT);
        }else if (value == 1) {
            return new WriteCellData<>(CHILD);
        }

        return new WriteCellData<>("");
    }
}
